/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

import {
  AfterContentInit,
  ChangeDetectionStrategy,
  Component,
  ContentChildren,
  Input,
  QueryList,
  TemplateRef,
  ViewEncapsulation
} from '@angular/core';
import { InputBoolean, NgClassType, ApesSizeLDSType } from '@shared/apes/core';
import { ApesInputDirective } from './apes-input.directive';

@Component({
  selector: 'apes-input-group',
  exportAs: 'apesInputGroup',
  preserveWhitespaces: false,
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './apes-input-group.component.html',
  host: {
    '[class.apes-input-group-compact]': 'apesCompact',
    '[class.apes-input-search-enter-button]': 'apesSearch',
    '[class.apes-input-search]': 'apesSearch',
    '[class.apes-input-search-sm]': 'isSmallSearch',
    '[class.apes-input-affix-wrapper]': 'isAffixWrapper',
    '[class.apes-input-group-wrapper]': 'isAddOn',
    '[class.apes-input-group]': 'isGroup',
    '[class.apes-input-group-lg]': 'isLargeGroup',
    '[class.apes-input-group-wrapper-lg]': 'isLargeGroupWrapper',
    '[class.apes-input-affix-wrapper-lg]': 'isLargeAffix',
    '[class.apes-input-search-lg]': 'isLargeSearch',
    '[class.apes-input-group-sm]': 'isSmallGroup',
    '[class.apes-input-affix-wrapper-sm]': 'isSmallAffix',
    '[class.apes-input-group-wrapper-sm]': 'isSmallGroupWrapper'
  }
})
export class ApesInputGroupComponent implements AfterContentInit {
  @ContentChildren(ApesInputDirective) listOfApesInputDirective: QueryList<ApesInputDirective>;
  private _size: ApesSizeLDSType = 'default';
  @Input() apesAddOnBeforeIcon: NgClassType;
  @Input() apesAddOnAfterIcon: NgClassType;
  @Input() apesPrefixIcon: NgClassType;
  @Input() apesSuffixIcon: NgClassType;
  @Input() apesAddOnBefore: string | TemplateRef<void>;
  @Input() apesAddOnAfter: string | TemplateRef<void>;
  @Input() apesPrefix: string | TemplateRef<void>;
  @Input() apesSuffix: string | TemplateRef<void>;
  @Input() @InputBoolean() apesSearch = false;
  @Input() @InputBoolean() apesCompact = false;

  @Input() set apesSize(value: ApesSizeLDSType) {
    this._size = value;
    this.updateChildrenInputSize();
  }

  get apesSize(): ApesSizeLDSType {
    return this._size;
  }

  get isLarge(): boolean {
    return this.apesSize === 'large';
  }

  get isSmall(): boolean {
    return this.apesSize === 'small';
  }

  get isAffix(): boolean {
    return !!(this.apesSuffix || this.apesPrefix || this.apesPrefixIcon || this.apesSuffixIcon);
  }

  get isAddOn(): boolean {
    return !!(this.apesAddOnAfter || this.apesAddOnBefore || this.apesAddOnAfterIcon || this.apesAddOnBeforeIcon);
  }

  get isAffixWrapper(): boolean {
    return this.isAffix && !this.isAddOn;
  }

  get isGroup(): boolean {
    return !this.isAffix && !this.isAddOn;
  }

  get isLargeGroup(): boolean {
    return this.isGroup && this.isLarge;
  }

  get isLargeGroupWrapper(): boolean {
    return this.isAddOn && this.isLarge;
  }

  get isLargeAffix(): boolean {
    return this.isAffixWrapper && this.isLarge;
  }

  get isLargeSearch(): boolean {
    return this.apesSearch && this.isLarge;
  }

  get isSmallGroup(): boolean {
    return this.isGroup && this.isSmall;
  }

  get isSmallAffix(): boolean {
    return this.isAffixWrapper && this.isSmall;
  }

  get isSmallGroupWrapper(): boolean {
    return this.isAddOn && this.isSmall;
  }

  get isSmallSearch(): boolean {
    return this.apesSearch && this.isSmall;
  }

  updateChildrenInputSize(): void {
    if (this.listOfApesInputDirective) {
      this.listOfApesInputDirective.forEach(item => (item.apesSize = this.apesSize));
    }
  }

  ngAfterContentInit(): void {
    this.updateChildrenInputSize();
  }
}
